﻿using ExcelProcessingLibrary;
using ExcelProcessingLibrary.DataStructure;
using System.Collections.Generic;
using System.Data;
using System.Windows;
using InteropExcel = Microsoft.Office.Interop.Excel;

namespace ExcelWork
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ButtonGroupExcel_Click(object sender, RoutedEventArgs e)
        {
            InteropExcel.Application application = new InteropExcel.Application();
            application.Visible = true;
            InteropExcel.Workbook workbook = application.Workbooks.Add();
            InteropExcel.Worksheet worksheet = workbook.Sheets[1] as InteropExcel.Worksheet;            

            ReportConfiguration reportConfig = new ReportConfiguration();
            reportConfig.TableStartRow = 5;
            //If header is hidden and CreateExcelTable is set to true, then create Excel table by considering (SheetStartRow - 1) as header row.  
            reportConfig.CreateExcelTable = true;
            reportConfig.ExcelTableConfiguration = new ExcelTableConfiguration();
            reportConfig.ExcelTableConfiguration.Name = "MyExcelTable";
            reportConfig.ExcelTableConfiguration.TableStyle = "TableStyleMedium2";
            reportConfig.ExcelTableConfiguration.ShowAutoFilter = true;

            List<ColumnConfiguration> reportColumnList = new List<ColumnConfiguration>();

            ColumnConfiguration columnConfig = new ColumnConfiguration();
            columnConfig.UniqueKey = "1";
            columnConfig.Hidden = false;
            columnConfig.DataField = "ParentGroup";
            columnConfig.HeaderHidden = false;
            columnConfig.HeaderText = "Parent Group";
            columnConfig.SheetColumn = "A";
            columnConfig.IsGroupingEnabled = true;
            columnConfig.GroupRank = 1;
            columnConfig.IsSubTotalEnabled = true;
            columnConfig.ColumnWidth = 5;
            reportColumnList.Add(columnConfig);

            columnConfig = new ColumnConfiguration();
            columnConfig.UniqueKey = "2";
            columnConfig.Hidden = false;
            columnConfig.DataField = "GroupName";
            columnConfig.HeaderHidden = false;
            columnConfig.HeaderText = "Group Name";
            columnConfig.Padding = 0;
            columnConfig.SheetColumn = "B";
            columnConfig.IsGroupingEnabled = true;
            columnConfig.GroupRank = 2;
            columnConfig.HideGroupValue = true;
            columnConfig.IsSubTotalEnabled = true;
            columnConfig.ColumnWidth = 5;
            reportColumnList.Add(columnConfig);

            columnConfig = new ColumnConfiguration();
            columnConfig.UniqueKey = "3";
            columnConfig.DataField = "GroupValue";
            columnConfig.HeaderHidden = false;
            columnConfig.HeaderText = "Group Value";
            columnConfig.Padding = 0;
            columnConfig.SheetColumn = "C";
            reportColumnList.Add(columnConfig);

            columnConfig = new ColumnConfiguration();
            columnConfig.UniqueKey = "4";
            columnConfig.Hidden = false;
            columnConfig.DataField = "Description";
            columnConfig.HeaderHidden = false;
            columnConfig.HeaderText = "Description";
            columnConfig.SheetColumn = "D";
            reportColumnList.Add(columnConfig);

            columnConfig = new ColumnConfiguration();
            columnConfig.UniqueKey = "5";
            columnConfig.DataField = "Par";
            columnConfig.HeaderHidden = false;
            columnConfig.HeaderText = "Par($)";
            columnConfig.SheetColumn = "E";
            columnConfig.ShowSubTotal = true;
            columnConfig.IsSubTotalAtGroupRow = true;
            columnConfig.TotalType = TotalType.Sum;
            columnConfig.WeightColumn = "F";
            columnConfig.ShowGrandTotal = true;
            columnConfig.IsGrandTotalAtStartRow = true;
            columnConfig.ExcelDataType = ExcelDataType.Currency;
            reportColumnList.Add(columnConfig);

            columnConfig = new ColumnConfiguration();
            columnConfig.UniqueKey = "6";
            columnConfig.Hidden = false;
            columnConfig.DataField = "Coupan";
            columnConfig.HeaderHidden = false;
            columnConfig.HeaderText = "Coupan(%)";
            columnConfig.SheetColumn = "F";
            columnConfig.ShowSubTotal = true;
            columnConfig.IsSubTotalAtGroupRow = true;
            columnConfig.TotalType = TotalType.Sum;
            columnConfig.ShowGrandTotal = true;
            columnConfig.IsGrandTotalAtStartRow = true;
            columnConfig.ExcelDataType = ExcelDataType.Percentage;
            reportColumnList.Add(columnConfig);

            reportConfig.ColumnConfigurations = reportColumnList;

            DataSet dataSet = new DataSet();
            dataSet.ReadXml(@"TestData/XMLData.xml");
            reportConfig.DataSource = dataSet.Tables[0];

            ExcelGroupEngine excelGroupEngine = new ExcelGroupEngine();
            excelGroupEngine.GenerateGroupReport(worksheet, reportConfig);
        }
    }
}
